Public Class FormUser

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim frm As New FormUserAED()

        frm.LoadRoleByUser(0)
        frm.chkLocked.Hide()
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            LoadData()
        End If
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = dgv.SelectedRows(0)

        Dim frm As New FormUserAED()
        frm.txtUserName.Enabled = False

        frm.txtUserId.Text = row.Cells("UserId").Value
        frm.txtUserName.Text = row.Cells("UserName").Value
        frm.txtFullName.Text = row.Cells("FullName").Value
        frm.txtEmail.Text = row.Cells("Email").Value
        frm.txtPassword.Text = frm.PasswordMashText
        frm.chkLocked.Checked = row.Cells("Locked").Value
        frm.chkChangePasswordAtLogon.Checked = row.Cells("ChangePasswordAtLogOn").Value
        frm.LoadRoleByUser(row.Cells("UserId").Value)
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            LoadData()
        End If
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Dim row = dgv.SelectedRows(0)
        Dim id = row.Cells(0).Value

        CmdPro.Parameters("@Id") = id
        CmdPro.ExecuteNonQueryNoParameter("DeleteUser")

        LoadData()

    End Sub

    Private Sub LoadData()
        CmdPro.Parameters("@S") = "%" & txtSearch.Text & "%"
        Me.dgv.DataSource = CmdPro.ExecuteDataTable("QueryUser")
        LoackUserColor()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dbinterface.OpenConnection()
        LoadData()
    End Sub

    Private Sub FormUser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoackUserColor()
    End Sub
    Public Sub LoackUserColor()
        For Each row As DataGridViewRow In dgv.Rows
            If row.Cells("Locked").Value = True Then

                row.Cells("UserName").Style.ForeColor = Color.Red
                row.Cells("FullName").Style.ForeColor = Color.Red
                row.Cells("Email").Style.ForeColor = Color.Red
                row.Cells("Locked").Style.ForeColor = Color.Red

            End If
        Next
    End Sub
End Class